package com.goldze.mvvmhabit.ui.tab_bar.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.adapter.ImageAdapter;
import com.goldze.mvvmhabit.bean.NewsBean;
import com.goldze.mvvmhabit.bean.RxValue;
import com.goldze.mvvmhabit.databinding.FragmentTabBar1Binding;
import com.goldze.mvvmhabit.okgo.HTTPAPI;
import com.goldze.mvvmhabit.rxBus.RxBus;
import com.goldze.mvvmhabit.ui.news.NewsDetailActivity;
import com.goldze.mvvmhabit.utils.GsonUtil;
import com.goldze.mvvmhabit.utils.SpUtils;
import com.goldze.mvvmhabit.view.BannerView;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.LogUtil;
import me.goldze.mvvmhabit.utils.ToastUtils;
import okhttp3.Call;
import okhttp3.Response;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by goldze on 2018/7/18.
 */

public class TabBar1Fragment extends Fragment {

    FragmentTabBar1Binding binding;
    String uname, tel, schoolName, age, status,temp,personId,number,classId,trip14,hot,lat,lng,isTouch="0",connectName;
    ImageAdapter imageAdapter;
    private List<String> mList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_tab_bar_1, container, false);
        binding.setFragment(this);
        initData();
        return binding.getRoot();
    }

    private void initData() {
        Subscription subscription = RxBus.getInstance()
                .tObservable(RxValue.class)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<RxValue>() {
                    @Override
                    public void call(RxValue model) {
                       lat = model.getValue().split(" ")[0];
                       lng = model.getValue().split(" ")[1];
                    }
                });
        RxBus.getInstance().addSubscription(this, subscription);

        uname = (String) SpUtils.get("module_name", "");
        tel = (String) SpUtils.get("module_tel", "");
        schoolName = (String) SpUtils.get("module_schoolname", "");
        age = (String) SpUtils.get("module_age", "");
        number = (String) SpUtils.get("module_student_number", "");
        classId = (String) SpUtils.get("module_class_id","");
        personId = (String) SpUtils.get("module_person_id","");

        binding.tvModuleName.setText(uname);
        binding.tvModuleTel.setText(tel);

        //14天省内？
        binding.rgModule.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbModuleFirst:
                        isTouch = "1";
                        status = "1";
                        break;
                    case R.id.rbModuleSecond:
                        isTouch = "0";
                        status = "0";
                        break;
                    default:
                }
            }
        });

        //身体发烧？
        binding.rgModuleBody.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbModuleFirst:
                        hot = "1,2,3";
                        status = "1";
                        break;
                    case R.id.rbModuleSecond:
                        hot = "";
                        status = "0";
                        break;
                    default:
                }
            }
        });


        //提交打卡点击事件
        binding.tvModuleOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                card();
            }
        });

        getNewList();
    }

    //获取新闻
    public void getNewList(){
        String token = (String) SpUtils.get("module_token","");
        HTTPAPI.getInstance().getNewsList(token,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, final Response response) {
                        LogUtil.e("新闻列表->" + s);
                        final NewsBean newsBean = GsonUtil.INSTANCE.parse(s,NewsBean.class);
                        if (newsBean.getCode() == 200) {
                            mList.clear();
                            for (NewsBean.ResultBean resultBean : newsBean.getResult()){
                                mList.add(resultBean.getPic());
                            }

                            binding.bvModule.setVisibility(View.VISIBLE);
                            binding.bvModule.setOnItemClickListener(new BannerView.OnItemClickListener() {
                                @Override
                                public void onItemClick(int position) {
                                    for (NewsBean.ResultBean resultBean : newsBean.getResult()){
                                        if (resultBean.getPic().equals(mList.get(position))) {
                                            startActivity(new Intent(getActivity(), NewsDetailActivity.class)
                                            .putExtra("module_id",resultBean.getId()));
                                        }
                                    }
                                }
                            }).setBannerData(mList).setSmoothInterval(1500).startSmoothAuto();
                        }

                    }
                });
    }

    //打卡
    public void card(){

        trip14 = binding.etModuleWay.getText().toString().trim();
        if (TextUtils.isEmpty(trip14)) {
            ToastUtils.showShort("请输入14天行程");
            return;
        }
        temp = binding.etModuleDu.getText().toString().trim();
        if (TextUtils.isEmpty(tel)) {
            ToastUtils.showShort("请输入体温");
            return;
        }

        double t = Double.parseDouble(temp);
        if (t > 38.3) {
            status = "1";
        }else{
            status = "0";
        }

        connectName = binding.etModuleContactName.getText().toString().trim();
        if (TextUtils.isEmpty(connectName)) {
            ToastUtils.showShort("请输入紧急联系人");
            return;
        }

        if (!binding.rbModuleFifth.isChecked()) {
            ToastUtils.showShort("请同意郑重承诺第一条");
            return;
        }
        if (!binding.rbModuleSixth.isChecked()) {
            ToastUtils.showShort("请同意郑重承诺第二条");
            return;
        }
        /**
         * 打卡的方法
         * @param name
         * @param tel
         * @param trip14            随便填
         * @param symptoms          发烧咳嗽
         * @param clazzId
         * @param lat
         * @param lng
         * @param isTouch           是否有接触感染人员
         * @param contactName       紧急联系人
         * @param callback
         */
        String token = (String) SpUtils.get("module_token","");

        HTTPAPI.getInstance().playCard(status,temp,token,uname,tel,trip14,hot,classId,lat,lng,isTouch,connectName,
                personId,new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.e("打卡-->" + s);
                        ToastUtils.showShort("打卡成功");
                        binding.rbModuleFirst.setChecked(true);
                        binding.rbModuleThird.setChecked(true);
                        binding.etModuleContactName.setText("");
                        binding.etModuleWay.setText("");
                    }
                });

    }

}
